/**
 * UserAction.java
*Administrator
*2012-3-28
 */
package com.free.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.free.constants.FrameworkConstant;
import com.free.logic.service.impl.IUserService;
import com.free.tools.JSONUtils;

/**
 * @author Administrator
 *
 */
@Controller
@Scope("prototype")
@Action(value = "user")
public class UserAction extends BaseAction {
	@Autowired
	private IUserService userService;
	
	public void findAllUser()
	{
		try
		{
			HttpServletResponse response = ServletActionContext.getResponse();
			super.setResonse();
			Map<String , Object> result = userService.findUser(dynamicForm);
			
			JSONObject jsonObject = JSONUtils.parseToObject(result);
			PrintWriter pw = response.getWriter();
			pw.write(jsonObject.toString());
			pw.flush();
			pw.close();
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
	}
	
	public void saveUser()
	{
		try
		{
			boolean saveResult = userService.saveUser(dynamicForm);
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter pw = response.getWriter();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("success", saveResult);
			jsonObject.put("msg", saveResult ? "保存成功" : "保存失败");
			pw.write(jsonObject.toString());
			pw.flush();
			pw.close();
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
	}
	
	public void editUser()
	{
		try
		{
			HttpServletResponse response = ServletActionContext.getResponse();
			super.setResonse();
			boolean editResult = userService.editUser(dynamicForm);
			PrintWriter pw = response.getWriter();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("success", editResult);
			jsonObject.put("msg", editResult ? "修改成功" : "修改失败");
			pw.write(jsonObject.toString());
			pw.flush();
			pw.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public void removeUser()
	{
		try
		{
			HttpServletResponse response = ServletActionContext.getResponse();
			super.setResonse();
			boolean removeResult = userService.removeUser(dynamicForm);
			PrintWriter pw = response.getWriter();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("success", removeResult);
			jsonObject.put("msg", removeResult ? "删除成功" : "删除失败");
			pw.write(jsonObject.toString());
			pw.flush();
			pw.close();
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
	}
}
